//
//  ELCImagePickerController.m
//  ELCImagePickerDemo
//
//  Created by Collin Ruffenach on 9/9/10.
//  Copyright 2010 ELC Technologies. All rights reserved.
//

#import "ELCImagePickerController.h"
#import "ELCAsset.h"
#import "ELCAssetCell.h"
#import "ELCAssetTablePicker.h"
#import "ELCAlbumPickerController.h"
#import "UIImage+Resize.h"
#import "stringAddition.h"
#import "UIImage+Video.h"


@implementation ELCImagePickerController
@synthesize photoFolderName = _photoFolderName;
@synthesize curSaveAssetArray = _curSaveAssetArray;

@synthesize delegate;

-(void)cancelImagePicker {

	if([delegate respondsToSelector:@selector(elcImagePickerControllerDidCancel:)]) {
		[delegate performSelector:@selector(elcImagePickerControllerDidCancel:) withObject:self];
	}
}

-(void)cancelImagePickerForAlbum {
    
	if([delegate respondsToSelector:@selector(elcImagePickerControllerDidAdd:)]) {
		[delegate performSelector:@selector(elcImagePickerControllerDidAdd:) withObject:self];
	}
}

- (NSString*)getImageExt:(ALAsset*)asset
{
    NSURL* photoURL = [[asset valueForProperty:ALAssetPropertyURLs] valueForKey:[[[asset valueForProperty:ALAssetPropertyURLs] allKeys] objectAtIndex:0]];
    NSString* imageURL = [photoURL absoluteString];
    NSRange extRange = [imageURL rangeOfString:@"&ext=" options:NSBackwardsSearch];
    if(extRange.location != NSNotFound)
    {
        
        NSString* extString = [[imageURL substringFromIndex:extRange.location+extRange.length] lowercaseString];
        NSLog(@"%@ --- %@", extString, [extString lowercaseString]);
        if(extString != nil && [extString length] > 0)
        {
            return extString;
        }
    }
    
    return @"jpg";
}

-(void)selectedAssets:(NSArray*)_assets {

	NSMutableArray *returnArray = [[[NSMutableArray alloc] init] autorelease];
	
	for(ALAsset *asset in _assets) {

		NSMutableDictionary *workingDictionary = [[NSMutableDictionary alloc] init];
		[workingDictionary setObject:asset forKey:@"SourceAsset"];
        
        [workingDictionary setObject:[self getImageExt:asset] forKey:@"image_ext"];
		
		[returnArray addObject:workingDictionary];
		
		[workingDictionary release];	
	}
	
    [self popToRootViewControllerAnimated:NO];
    [[self parentViewController] dismissModalViewControllerAnimated:YES];
    
	if([delegate respondsToSelector:@selector(elcImagePickerController:didFinishPickingMediaWithInfo:)]) {
		[delegate performSelector:@selector(elcImagePickerController:didFinishPickingMediaWithInfo:) withObject:self withObject:[NSArray arrayWithArray:returnArray]];
	}
}


//得到选择的相册中的asset
- (void)getPhotoAssetInAlbum:(NSMutableArray*)ablumArray
{
    for(ALAssetsGroup* assetsGroup in ablumArray)
    {
        [assetsGroup enumerateAssetsUsingBlock:^(ALAsset *result, NSUInteger index, BOOL *stop) 
         {         
             if(result == nil) 
             {
                 return;
             }
             
             [_curSaveAssetArray addObject:result];
             
         }];
    }
}



//少于20张直接添加
- (void)savePhotosAssets:(NSMutableArray*)albumArray
{
    if([_curSaveAssetArray count] != 0)
    {
        [_curSaveAssetArray removeAllObjects];
    }
    
    [self getPhotoAssetInAlbum:albumArray];
    [self selectedAssets:_curSaveAssetArray];
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    _curImageIndex = 0;
    _curSaveCount = 0;
    self.curSaveAssetArray = [NSMutableArray array];
}

#pragma mark -
#pragma mark Memory management

- (void)didReceiveMemoryWarning {    
    NSLog(@"ELC Image Picker received memory warning!!!");
    
    [super didReceiveMemoryWarning];
}

- (void)viewDidUnload {
    [super viewDidUnload];
}


- (void)dealloc {
    NSLog(@"deallocing ELCImagePickerController");
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    [_photoFolderName release];
    [_curSaveAssetArray release];
    [super dealloc];
}

@end
